package com.jgame.secondarymarket.domain.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.jgame.secondarymarket.domain.account.entity.Account;
import com.jgame.secondarymarket.domain.account.repository.AccountRepository;
import com.jgame.secondarymarket.domain.account.service.AccountService;
import com.jgame.secondarymarket.infrastructure.common.model.enums.Role;
import com.jgame.secondarymarket.infrastructure.excetpions.BussnessException;
import com.jgame.secondarymarket.infrastructure.util.Encryption;
import com.jgame.secondarymarket.interfaces.dto.account.AccountDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @title: AccountServiceImpl
 * @Author Wangsj
 * @Date: 2021/7/30
 */

@Service
@Slf4j
public class AccountServiceImpl implements AccountService {

    @Autowired
    AccountRepository accountRepository;

    @Autowired
    private Encryption encoder;

    @Override
    public void saveAccount(AccountDTO accountDTO) {
        if (null != accountRepository.findByUserName(accountDTO.getUserName())) {
            throw BussnessException.throwException("该用户名已存在");
        }
        accountDTO.setPassword(encoder.encode(accountDTO.getPassword()));
        Account account = new Account();
        BeanUtil.copyProperties(accountDTO, account);
        //默认为用户权限
        account.setRole(Role.USER.getRole());
        accountRepository.save(account);
    }
}
